#include "tcp_client.h"

tcp_client::tcp_client(asio::io_context& context):net_tcp::client(context)
{
}

void tcp_client::on_connected()
{
	if (m_on_session_connect != nullptr)
	{
		m_on_session_connect(m_session->ID());
	}
}

void tcp_client::on_disconnected()
{
	if (m_on_session_disconnect != nullptr)
	{
		m_on_session_disconnect(m_session->ID());
	}
}

void tcp_client::on_recv()
{
	if (m_on_session_message != nullptr)
	{
		net_tcp::buffer& recv_buffer = m_session->get_recv_buffer();
		int readlen = m_on_session_message(m_session->ID(), recv_buffer.read_pointer(), recv_buffer.readable_size());
		if (readlen > 0)
		{
			recv_buffer.increase_read(readlen);
		}
	}
}

void tcp_client::on_error()
{
	if (m_on_session_error != nullptr)
	{
		asio::error_code error = m_session->get_last_error();
		m_on_session_error(m_session->ID(), error.value());
	}
}
